<?php
	$SERVICES = array (
		'twitter.com' => 'twitter',
		'livejournal.com' => 'livejournal',
		'friendfeed.com' => 'friendfeed',
		'upcoming.yahoo.com' => 'upcoming',
		'dopplr.com' => 'dopplr',
		'facebook.com' => 'facebook',
		'flickr.com' => 'flickr',
		'linkedin.com' => 'linkedin',
		'last.fm' => 'lastfm',
		'youtube.com' => 'youtube',
		'pownce.com' => 'pownce',
		'digg.com' => 'digg',
		'del.icio.us' => 'delicious',
		
	);

	function stalk_guess_service($url)
	{
		global $SERVICES;

		foreach ($SERVICES as $u => $s)
			if (strpos($url, $u)!==false)
				return $s;

		return 'unknown';
	}
	

	function get_service_icon_url($service, $size = "small")
	{
		global $CONFIG;

		$icon = $CONFIG->url . "gfx/$size/$service.png";
		if (@file_get_contents($icon))
			return $icon;
			
		return $CONFIG->url . "gfx/$size/unknown.png";
	}
	
	/**
	 * Simple per thread cache for these wrapper functions.
	 *
	 * @param unknown_type $service
	 * @param unknown_type $url
	 * @param unknown_type $result
	 */
	function wrapper_cache_query($func, $service, $url, $result)
	{
		static $WRAPPER_QUERY_CACHE;
		
		if (!$WRAPPER_QUERY_CACHE)
			$WRAPPER_QUERY_CACHE = array();
			
		$WRAPPER_QUERY_CACHE[md5($func.$service.$url)] = $result;
	}
	
	/**
	 * Simple per thread cache for these wrapper functions.
	 *
	 * @param unknown_type $service
	 * @param unknown_type $url
	 * @param unknown_type $result
	 */
	function wrapper_get_cached_query($func, $service, $url) {
		static $WRAPPER_QUERY_CACHE;
		
		if (!$WRAPPER_QUERY_CACHE)
			$WRAPPER_QUERY_CACHE = array();
			
		return $WRAPPER_QUERY_CACHE[md5($func.$service.$url)];
	}
	
	function wrapper_call_function($func, $service, $url)
	{
		$cached = wrapper_get_cached_query($func, $service, $url);
		if ($cached) 
			return $cached;
		
		if (is_callable($func)) {
			$result = $func($url);
			wrapper_cache_query($func, $service, $url, $result);
			
			return $result;
		}
		
		return false;
	}

	/**
	 * Get the friends for a given service. This function in turn calls a function
	 * 
	 * get_friends_$service($url)
	 *
	 * @return an array of stdclass with ->url, ->username, & ->name filled
	 */
	function get_friends($service, $url)
	{
		$func = "get_friends_$service";
		
		return wrapper_call_function($func, $service, $url);
	}
	
	
	function get_header($service)
	{
		$func = "get_header_$service";
		if (is_callable($func))
			return $func();

		return false;
		
	}
	
	function get_user_icon( $service, $url)
	{
		$func = "get_user_icon_$service"; 
		if (is_callable($func))
			$return = $func($url);

		if (!$return)
			return get_service_icon_url($service, 'large');

		return $return;
	}


	function get_location($service, $url)
	{
		$func = "get_location_$service";
		
		return wrapper_call_function($func, $service, $url);

	}


	function get_tags($service, $url)
	{
		$func = "get_tags_$service";
		
		return wrapper_call_function($func, $service, $url);

	}
	
	/**
	 * Get the rendered contents from a service
	 *
	 * @param string $url
	 * @param string $service Optional service
	 * @return rendered html
	 */
	function get_rendered_contents($url, $service = "")
	{
		global $LAST_UPDATED;
		
		// Short circuit if a service specific renderer is present
		if (($service) && (is_callable("get_rendered_contents_$service"))) {
			$func = "get_rendered_contents_$service";

			return $func($url);
		
		}
			
		$sg = socialgraph_query($url);

		if (isset($sg->nodes->$url))
		{
			$return = "";
			$node = $sg->nodes->$url;

			if (isset($node->attributes->rss))
			{
			
				$rss = fetch_rss($node->attributes->rss);
				$n = 0;

				if(!$LAST_UPDATED) $LAST_UPDATED = array();
				$LAST_UPDATED[$url] = 0;
				
				foreach ($rss->items as $node)
				{
					$t = strtotime($node['pubdate']);
					if ($t>$LAST_UPDATED[$url]) $LAST_UPDATED[$url]=$t;
					
					if ($n<15) {
						if (strcmp($node['title'], $node['description'])!=0) 
							$desc = "<div id=\"description\">{$node['description']}</div>";
						else
							$desc = "";
							
						$return .= <<< END
					<div class="rss_item">
						<div id="title">{$node['title']}</div>
						$desc
						<div id="date">{$node['pubdate']}</div>
					</div>
END;
						$n++;
					}
				}
			}
			
			return $return;
		}

		return false;
	}
	

	function get_last_updated($url)
	{
		global $LAST_UPDATED;
		
		if (!isset($LAST_UPDATED[$url]))
			get_rendered_contents($url);
			
		return $LAST_UPDATED[$url];
	}
	
	function get_username_from_url($service, $url)
	{
		$func = "get_username_from_url_$service";
		return wrapper_call_function($func, $service, $url);
	}
?>
